import { getCityList } from "@/api/logistics.js";
import { buildTree } from "@/utils/common.js";

const city = {
	 state: {
		cityList: uni.getStorageSync("cityList") || []
	  },
	  mutations: {
		setCityList(state, data) {
		  state.cityList = data;
		  uni.setStorageSync("cityList", data); // 更新本地缓存
		}
	  },
	  actions: {
		async fetchCityList({ state, commit }) {
			  if (state.cityList.length > 0) {
				return state.cityList; // 直接返回缓存数据
			  }
			const { code, rows } = await getCityList();
			 if(code == 200) {
				 commit("setCityList", buildTree(rows));
				 return buildTree(rows);
			 }
		}
	  }
}

export default city;